package com.ruoyi.project.system.ordertable.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.ordertable.domain.Ordertable;
import com.ruoyi.project.system.ordertable.service.IOrdertableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-03-31
 */
@Controller
@RequestMapping("/system/ordertable")
public class OrdertableController extends BaseController
{
    private String prefix = "system/ordertable";

    @Autowired
    private IOrdertableService ordertableService;




    @RequiresPermissions("system:ordertable:view")
    @GetMapping("/read/{idorderHead}")
    public String ordertable(@PathVariable("idorderHead") Integer idorderHead)
    {
        System.out.println("--------------"+idorderHead);
        Session session = ShiroUtils.getSession();
        session.setAttribute("id",idorderHead);
        return prefix + "/ordertable";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:ordertable:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Ordertable ordertable)
    {
        Session session = ShiroUtils.getSession();
        Integer id = (Integer)session.getAttribute("id");
        if(id != null){
           ordertable.setHeaderIDOrder(id.longValue());
        }
        startPage();
       // List<Ordertable> list = ordertableService.selectOrdertableList(ordertable);
        List<Map<String, Object>> readList = ordertableService.getReadOrderDataList(id);
        return getDataTable(readList);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:ordertable:export")
    @Log(title = "订单商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Ordertable ordertable)
    {
        List<Ordertable> list = ordertableService.selectOrdertableList(ordertable);
        ExcelUtil<Ordertable> util = new ExcelUtil<Ordertable>(Ordertable.class);
        return util.exportExcel(list, "ordertable");
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增【请填写功能名称】
     */
    @PostMapping("/addpro")
    public String add( ModelMap mmap)
    {
       // mmap.put("idorderHead",idorderHead);
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:ordertable:add")
    @Log(title = "订单商品新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Ordertable ordertable)
    {
        Session session = ShiroUtils.getSession();
        Integer id = (Integer)session.getAttribute("id");
        if(id != null){
            ordertable.setHeaderIDOrder(id.longValue());
        }
        return toAjax(ordertableService.insertOrdertable(ordertable));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{idOrder}")
    public String edit(@PathVariable("idOrder") Integer idOrder, ModelMap mmap)
    {
       // Ordertable ordertable = ordertableService.selectOrdertableById(idOrder);
        Ordertable ordertable = ordertableService.queryOrdertableOne(idOrder);
        mmap.put("ordertable", ordertable);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:ordertable:edit")
    @Log(title = "订单商品修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Ordertable ordertable)
    {
        return toAjax(ordertableService.updateOrdertable(ordertable));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:ordertable:remove")
    @Log(title = "订单商品删除", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ordertableService.deleteOrdertableByIds(ids));
    }
}
